<template>

  <div class="contener">
    <span>wuchuanchuan</span>
    <el-form :inline="true" :model="queryForm" ref="ruleFormRef">
      <el-form-item label="学科名称" prop="menuName">
        <el-input v-model="queryForm.subjectName" placeholder="请输入学科名称" size="small"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-refresh" @click="resetForm" size="mini">清除</el-button>
        <el-button type="primary" icon="el-icon-search" @click="queryStudy" size="mini">搜索</el-button>

      </el-form-item>
      <el-form-item>
        <span style="padding-left: 700px">
          <el-button type="success" icon="el-icon-circle-plus" size="mini" @click="addSubject">新增学科
          </el-button>
        </span>
      </el-form-item>
    </el-form>
    <el-alert type="warning" show-icon>
      <span>数据共{{total}}条</span>
    </el-alert>
    <!-- 表格 -->
    <el-table :data="tableList" style="width: 100%">
      <el-table-column type="index" label="序号" width="100">
      </el-table-column>
      <el-table-column prop="subjectName" label="学科名称" width="150">
      </el-table-column>
      <el-table-column prop="username" label="创建者" width="150">
      </el-table-column>
      <el-table-column prop="addDate" :formatter="dateFormat" label="创建日期" width="150">
      </el-table-column>
      <el-table-column prop="isFrontDisplay" align="center" label="前台是否显示" width="150">
        <template slot-scope="scope">
          <span v-if="scope.row.isFrontDisplay=='1'" plain size="mini">是</span>
          <span v-if="scope.row.isFrontDisplay=='0'" plain size="mini">否</span>
        </template>
      </el-table-column>
      <el-table-column prop="twoLevelDirectory" label="二级目录" width="100">
      </el-table-column>
      <el-table-column prop="tags" label="标签" width="100">
      </el-table-column>
      <el-table-column prop="totals" label="题目数量" width="100">
      </el-table-column>
      <el-table-column prop="" label="操作" width="300">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="clickSkip(scope.row.id)">学科分类</el-button>
          <el-button type="text" size="small" @click="skipClick(scope.row.id)">学科标签</el-button>
          <el-button @click="handleClick(scope.row)" type="text" size="small">修改</el-button>
          <el-popconfirm title="确定要永久删除此条数据吗？" @confirm="delStudy(scope.row.id)">
            <el-button slot="reference" size="small" type="text">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination background layout="prev, pager, next" @current-change="changePage" style="float: right"
      :total="total">
    </el-pagination>
    <!-- 新增学科 -->
    <el-dialog title="新增学科" :visible.sync="addDialogVisible" width="30%">
      <el-form :model="form" :rules="rules" ref="resetFormData">
        <el-form-item prop="subjectName" label="学科名称">
          <el-input v-model="form.subjectName" type="text" placeholder="请输入学科名称" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="isFrontDisplay">
          <el-switch style="display: block" v-model="form.isFrontDisplay" :active-value="1" :inactive-value="0"
            active-color="#13ce66" inactive-text="是否在页面显示">
          </el-switch>
        </el-form-item>
      </el-form>
      <el-button @click="addDialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="addSureDialogVisible">确 定</el-button>
    </el-dialog>
    <!-- 修改学科 -->
    <el-dialog title="修改学科" :visible.sync="editaddDialogVisible" width="30%">
      <el-form :model="form1" :rules="editRules" ref="resetFormData">
        <el-form-item prop="subjectName" label="学科名称">
          <el-input v-model="form1.subjectName" type="text" placeholder="请输入学科名称" autocomplete="off">
          </el-input>
        </el-form-item>
        <el-form-item prop="isFrontDisplay">
          <el-switch style="display: block" v-model="form1.isFrontDisplay" :active-value="1" :inactive-value="0"
            active-color="#13ce66" inactive-text="是否在页面显示">
          </el-switch>
        </el-form-item>
      </el-form>
      <el-button @click=" editaddDialogVisible=false">取 消</el-button>
      <el-button type="primary" @click="editSureDialogVisible">确 定</el-button>
    </el-dialog>

  </div>
</template>
<script>
import { addSetSubject } from '../../api/base/frame'
import { query, remove, list, update } from '../../api/hmmm/subjects'
import moment from 'moment'
export default {
  data () {
    return {
      value2: '',
      addDialogVisible: false,
      editaddDialogVisible: false,
      form: { subjectName: '', isFrontDisplay: '' },
      form1: { subjectName: '', isFrontDisplay: '' },
      tableList: [],
      total: 0,
      queryInfo: {
        page: 1,
        pageSize: 10,
        subjectName: ''
      },
      queryForm: {
        page: 1,
        pageSize: 10,
        subjectName: ''

      },
      data: {
        body: 1
      },
      rules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }

        ]
      },
      editRules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }

        ]
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 学科分类
    clickSkip (val) {
      this.$router.push({ path: 'directorys', query: { id: val } })
    },
    // 学科标签
    skipClick (val) {
      this.$router.push({ path: 'tags', query: { id: val } })
    },
    // 修改数据
    async editSureDialogVisible () {
      const { data: res } = await update(this.form1)
      if (res.success === true) {
        this.$message({
          message: '修改成功',
          type: 'success'
        })
        this.getList()
      } else {
        this.$message({
          message: '修改失败',
          type: 'error'
        })
      }

      this.editaddDialogVisible = false
    },
    resetForm () {
      this.$refs.ruleFormRef.resetFields()
    },
    // 新增学科
    async addSureDialogVisible () {
      const { data: res } = await addSetSubject(this.form)
      console.log(res)
      if (res !== undefined) {
        this.addDialogVisible = false
        this.$message({
          message: '新增成功',
          type: 'success'
        })
        this.form = {}
        this.getList()
      } else {
        this.$message({
          message: '新增失败',
          type: 'error'
        })
        this.form = {}
      }
    },
    Cancel () {

    },
    // 搜索学科
    async queryStudy () {
      const { data: res } = await query(this.queryForm)
      this.tableList = res.items
      this.total = res.counts
    },
    // 页码改变函数
    changePage (val) {
      this.queryInfo.page = val
      if (this.queryForm.subjectName !== '' || this.queryForm.subjectName !== undefined) {
        this.queryForm.page = val
        this.queryStudy()
      } else {
        this.getList()
      }
    },
    // 打开新增学科对话框
    addSubject () {
      this.addDialogVisible = true
    },
    // 获取数据列表
    async getList () {
      const { data: res } = await list(this.queryInfo)
      this.tableList = res.items
      this.total = res.counts
    },
    // 修改对话框
    handleClick (row) {
      this.form1 = { ...row }
      this.editaddDialogVisible = true
      // this.queryData.id = id
      console.log(row)
    },
    delStudy (val) {
      this.delData(val)
    },
    // 删除数据
    async delData (id) {
      const { data: res } = await remove({ id: id })
      if (res.success) {
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        this.getList()
      } else {
        this.$message({
          message: '删除失败',
          type: 'error'
        })
      }
    },
    // 格式化日期
    dateFormat (row, column) {
      var date = row[column.property]

      if (date === undefined) {
        return ''
      }

      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    }

  }
}
</script>

<style scoped lang='less'>
.el-row {
  margin: 20px 0;
  text-align: center;
}
.contener {
  margin: 0 15px;
  background-color: #ffffff;
}
</style>
